--------------------------------------------
-- 采集物
--------------------------------------------

local CollectExcel = require("excel.Collect")
local Obj = require("core.Obj")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Status = require("scene.Status")
local MapCallback = require("scene.MapCallback")

function create(collectId, ownerUuid)
	local cf = CollectExcel.collect[collectId]
	if not cf then
		assert(nil, collectId)
	end

	local collect = {}
	Obj.create(collect, Obj.TYPE_COLLECT)
	collect.collectId = collectId
	collect.ownerUuid = ownerUuid
	collect.way = cf.way
	return collect
end

function destroy(collect)
	Obj.destroy(collect)
end

function getObjAdd(collect)
	local collectId = collect.collectId
	local msgRet = Msg.gc.GC_COLLECT_ADD
	msgRet.obj_id = collect.id
	msgRet.collectId = collectId
	msgRet.x = collect.x
	msgRet.y = collect.y
	msgRet.way = collect.way
	return msgRet
end

function getSpeed()	
	return 0
end

function enterScene(collect, sceneID, x, y)
	Obj.enterScene(collect, sceneID, x, y)
end

function collectStart(human, obj_id, x, y, way)
	local collect = Obj.objs[obj_id]
	-- 不存在
	if not collect or	
		collect.obj_type ~= Obj.TYPE_COLLECT or
		collect.sceneID ~= human.sceneID then
		return
	end

	human.collect = human.collect or {}
	human.collect.startTime = os.time()
	human.collect.obj_id = obj_id
	human.x = x 
	human.y = y 
	human.way = way
	Status.doHumanControlStatusChange(human, Status.HUMAN_STATUS_COLLECT)

	local cf = CollectExcel.collect[collect.collectId]
	if cf and cf.tip1 ~= "" then
		Broadcast.sendErr(human, cf.tip1)
	end

	local msgRet = Msg.gc.GC_COLLECT_START
	msgRet.obj_id = obj_id
	msgRet.leftTime = cf.CollectTime
	Msg.send(msgRet, human.fd)
end

function collectFinish(human, obj_id)

	local collect = Obj.objs[obj_id]
	-- 不存在
	if not collect or	
		collect.obj_type ~= Obj.TYPE_COLLECT or
		collect.sceneID ~= human.sceneID then
		Status.doHumanControlStatusChange(human, Status.HUMAN_STATUS_NORMAL)
		return
	end

	local cf = CollectExcel.collect[collect.collectId]
	if not human.collect or
		human.collect.obj_id ~= obj_id or
		human.collect.startTime + cf.CollectTime > os.time() then
		Status.doHumanControlStatusChange(human, Status.HUMAN_STATUS_NORMAL)
		return
	end

	human.collect = nil
	if cf and cf.tip2 ~= "" then
		Broadcast.sendErr(human, cf.tip2)
	end

	Obj.leaveScene(collect)
	destroy(collect)

	Status.doHumanControlStatusChange(human, Status.HUMAN_STATUS_NORMAL)
	MapCallback.onCollectFinish(human, collect)

	local msgRet = Msg.gc.GC_COLLECT_FINISH
	msgRet.obj_id = obj_id
	Msg.send(msgRet, human.fd)
end





